//
//  NSString+YYAdd.m
//  YYKit <https://github.com/ibireme/YYKit>
//
//  Created by ibireme on 13/4/3.
//  Copyright (c) 2015 ibireme.
//
//  This source code is licensed under the MIT-style license found in the
//  LICENSE file in the root directory of this source tree.
//

#import "NSString+Util.h"
#import<CommonCrypto/CommonDigest.h>

@implementation NSString (Util)

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

- (CGFloat)widthForFont:(UIFont *)font {
    CGSize size = [self sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}

- (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width {
    CGSize size = [self sizeForFont:font size:CGSizeMake(width, HUGE) mode:NSLineBreakByWordWrapping];
    return size.height;
}

- (NSString *)stringByTrim {
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    return [self stringByTrimmingCharactersInSet:set];
}

//汉字的拼音
- (NSString *)pinyin{
    NSMutableString *str = [self mutableCopy];
    CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);

    return [str stringByReplacingOccurrencesOfString:@" " withString:@""];
}

- (BOOL)isTextNil
{
    if ([self isEqualToString:@""]||self.length == 0)
    {
        return YES;
    }
    return NO;
}

#pragma mark - 日期转型
- (NSString *)getDateInfo
{
    NSDate * date  = [NSDate date];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    NSString *dateStr = [NSString stringWithFormat:@"%@·%@月·%@日",[self changeChineseCharactersDay:[NSString stringWithFormat:@"%ld",comps.year]],[self changeChineseCharactersDay:[NSString stringWithFormat:@"%ld",comps.month]],[self changeChineseCharactersDay:[NSString stringWithFormat:@"%ld",comps.day]]];
    return dateStr;
}

- (NSString *)changeChineseCharactersDay:(NSString *)str
{
    NSMutableString *returnStr = [[NSMutableString alloc]init];
    //年
    if ([str integerValue]>31)
    {
        for(int i = 0; i < [str length]; i++)
        {
            NSString *temp = [self getBigCount:[str substringWithRange:NSMakeRange(i,1)]];
            [returnStr appendString:temp];
        }
    }
    else
    {
        if ([str integerValue] < 10)
        {
            return [self getBigCount:str];
        }
        else if([str integerValue]%10 == 0)
        {
            return [NSString stringWithFormat:@"%@十",[self getBigCountMonthAndDay:[str substringWithRange:NSMakeRange(0,1)]]];
        }
        else
        {
            return [NSString stringWithFormat:@"%@十%@",[self getBigCountMonthAndDay:[str substringWithRange:NSMakeRange(0,1)]],[self getBigCount:[str substringWithRange:NSMakeRange(1,1)]]];
        }
    }
    return returnStr;
}

- (NSString *)getBigCount:(NSString *)str
{
    switch ([str integerValue])
    {
        case 0:return @"〇";
        case 1:return @"一";
        case 2:return @"二";
        case 3:return @"三";
        case 4:return @"四";
        case 5:return @"五";
        case 6:return @"六";
        case 7:return @"七";
        case 8:return @"八";
        case 9:return @"九";
        default:return @"十";
    }
}

- (NSString *)getBigCountMonthAndDay:(NSString *)str
{
    if ([str integerValue] == 1)
    {
        return @"";
    }
    
    switch ([str integerValue])
    {
        case 0:return @"〇";
        case 1:return @"一";
        case 2:return @"二";
        case 3:return @"三";
        case 4:return @"四";
        case 5:return @"五";
        case 6:return @"六";
        case 7:return @"七";
        case 8:return @"八";
        case 9:return @"九";
        default:return @"十";
    }
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil)
    {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

- (NSString*)md532BitLower
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    
    NSNumber *num = [NSNumber numberWithUnsignedLong:strlen(cStr)];
    CC_MD5( cStr,[num intValue], result );
    
    return [[NSString stringWithFormat:
             @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}

- (NSString*)md532BitUpper
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    
    NSNumber *num = [NSNumber numberWithUnsignedLong:strlen(cStr)];
    CC_MD5( cStr,[num intValue], result );
    NSString *str = [[NSString stringWithFormat:
                      @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                      result[0], result[1], result[2], result[3],
                      result[4], result[5], result[6], result[7],
                      result[8], result[9], result[10], result[11],
                      result[12], result[13], result[14], result[15]
                      ] uppercaseString];
    
    return str;
}

- (BOOL)findPhotoUrl
{
    if([self hasSuffix:@".jpg"]||[self hasSuffix:@".png"]||[self hasSuffix:@".jpeg"]||[self hasSuffix:@".gif"])
    {
        return YES;
    }
    return NO;
}

@end
